package com.netease.nim.demo.main.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.netease.nim.demo.common.fragment.TabFragment;
import com.netease.nim.demo.main.model.MainTab;
import com.shwread.android.qysw10000227.R;

import logic.util.Utils;


public abstract class MainTabFragment
        extends
        TabFragment
{
	public static final int CREATE_TEAM = 0x7;
	public static String    UPDATE_NICK_NAME;
	private boolean         loaded      = false;
	
	private MainTab         tabData;
	
	protected abstract void onInit();
	
	protected abstract void updateList();
	
	protected boolean inited() {
		return loaded;
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		return inflater.inflate(R.layout.main_tab_fragment_container, container, false);
	}
	
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
	}
	
	public void attachTabData(MainTab tabData) {
		this.tabData = tabData;
	}
	
	@Override
	public void onCurrent() {
		super.onCurrent();
		
		if (!loaded && loadRealLayout())
		{
			loaded = true;
			onInit();
		}
	}
	
	private boolean loadRealLayout() {
		ViewGroup root = (ViewGroup) getView();
		if (root != null)
		{
			root.removeAllViewsInLayout();
			View.inflate(root.getContext(), tabData.layoutId, root);
		}
		return root != null;
	}
	
	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (!Utils.isEmpty(UPDATE_NICK_NAME) || requestCode == CREATE_TEAM)
		{
			UPDATE_NICK_NAME = "";
			updateList();
		}
	}
}
